enyo.kind({
    name: "MyApps.GolfClubs",
    kind: enyo.VFlexBox,
    events: {
        onBack: "",
    },
    components: [    /**
     * service
     */
    {
        name: "getData",
        kind: "WebService",
        url: "http://golfplus.mobile2you.fr/golfs.json",
        onSuccess: "gotData",
        onFailure: "gotDataFailure"
    },    /**
     * Layout
     */
    {
        name: "pane",
        kind: "Pane",
        flex: 1,
        components: [{
            kind: "VFlexBox",
            className: "whitebackground",
            components: [{
                name: "searchInput",
                kind: "SearchInput",
                hint: "Recherche",
                className: "searchinput",
                oninput: "searchTextEntered",
            
            }, {
                name: "list",
                kind: "VirtualList",
                className: "golfclubslist",
                flex: 1,
                onSetupRow: "listSetupRow",
                components: [{
                    kind: "Divider"
                }, {
                    kind: "Item",
                    layoutKind: "HFlexLayout",
                    components: [{
                        name: "itemLabel",
                        flex: 1
                    }, {
                        name: "rowLabel"
                    }],
                    onclick: "listItemClick"
                }]
            }, ]
        },        /**
         * Components definition
         */
        {
            name: "golfViewDetails",
            kind: "MyApps.GolfClubDetail",
            onBack: "goBack"
        }, ]
    }, ],
    create: function(){
        this.inherited(arguments);
        this.rawdata = [];
        this.data = [];
        this.golfData = [];
        this.$.getData.call();
    },
    gotData: function(inSender, inResponse){
        this.rawdata = inResponse;
        this.data = inResponse;
        this.$.list.refresh();
    },
    gotDataFailure: function(inSender, inResponse){
        console.log("got failure from getData");
    },
    getGroupName: function(inIndex){
        // get previous record
        var r0 = this.data[inIndex - 1];
        // get (and memoized) first letter of last name
        if (r0 && !r0.letter) {
            r0.letter = r0.golf.nom[0];
        }
        var a = r0 && r0.letter;
        // get record
        var r1 = this.data[inIndex];
        if (!r1.letter) {
            r1.letter = r1.golf.nom[0];
        }
        var b = r1.letter;
        // new group if first letter of last name has changed
        return a != b ? b : null;
    },
    setupDivider: function(inIndex){
        // use group divider at group transition, otherwise use item border for divider
        var group = this.getGroupName(inIndex);
        this.$.divider.setCaption(group);
        this.$.divider.canGenerate = Boolean(group);
        this.$.item.applyStyle("border-top", Boolean(group) ? "none" : "1px solid silver;");
    },
    listSetupRow: function(inSender, inRow){
        var f = this.data[inRow];
        if (f) {
        
            //if something is in searchinput
            var searchvalue = this.$.searchInput.getValue();
            if (searchvalue) {
                //dont need divider
                this.$.itemLabel.setContent(f.golf.nom);
            }
            else {
            
                this.setupDivider(inRow);
                this.$.itemLabel.setContent(f.golf.nom);
            }
            return true;
        }
    },
    listItemClick: function(inSender, inEvent){
        var golf = this.data[inEvent.rowIndex];
        this.$.pane.selectViewByName("golfViewDetails");
        this.$.golfViewDetails.setGolf(golf);
    },
    goBack: function(inSender, inEvent){
        this.$.pane.back(inEvent);
    },
    
    searchTextEntered: function(){
    
        //filter data
        this.data = [];
        //iterator
        var i = 0;
        var searchvalue = this.$.searchInput.getValue().toLowerCase();
        for (x in this.rawdata) {
            if (this.rawdata[x].golf.nom.toLowerCase().indexOf(searchvalue) === 0) {
                this.data[i++] = this.rawdata[x];
                
            }
        }
        
        this.$.list.refresh();
    },

});



